<%@ page import="java.io.* , java.util.*, java.sql.*, com.myapp.struts.dao.*,com.myapp.struts.actionform.*,com.myapp.struts.beans.*"%>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Item Master</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link href="../../../../css/cms.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <%
                String acheadtype = "";
                String openingBalance = "";
                String accountHead1 = request.getParameter("achead");
                System.out.println("account head" + accountHead1);
                AcheadDao imdao = new AcheadDao();
                ArrayList gp = imdao.acheadDetails(accountHead1);
                if (gp != null) {
                    Iterator itin = gp.iterator();
                    while (itin.hasNext()) {
                        System.out.println("Not Null");
                        AccountHeadBean gb = (AccountHeadBean) itin.next();
                        acheadtype = gb.getDescription();
                        System.out.println("acheadtype=" + acheadtype);
                        openingBalance = gb.getOBalance();
                        System.out.println("openingBalance=" + openingBalance);
                    }
                }
        %>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <th colspan="7" scope="col">Ledger </th>
            </tr>
            <tr>
                <td width="24%"><strong>Account head</strong></td>
                <td width="25%"><strong><%=accountHead1%>&nbsp;</strong></td>
                <td width="51" colspan="3">&nbsp;</td>
            </tr>
            <tr>
                <td><strong>Account Nature</strong></td>
                <td><strong><%=acheadtype%></strong></td>
                <td colspan="3">&nbsp;</td>
            </tr>
        </table>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="49%" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-right: #000000 1px solid;
                                                    border-top: #000000 1px solid; border-left: #000000 1px solid;  border-bottom: #000000 1px solid; font-size: 9pt; color: #000000; font-family: Arial;">
                        <tr>
                            <td colspan="4"><strong>Debit (Benifits Received By Him)</strong></td>
                        </tr>
                        <tr style="border-right: #000000 1px solid;
                            border-top: #000000 1px solid; border-left: #000000 1px solid;  border-bottom: #000000 1px solid; font-size: 9pt; color: #000000; font-family: Arial;">
                            <th width="11%" height="24">Date</th>
                            <th width="13%">Description</th>
                            <th width="5%">Ref</th>
                            <th width="20%">Amount</th>
                        </tr>
                        <%
                        if (acheadtype.equals("Dr")) {
                        %>
                        <tr >
                            <td align="center"></td>
                            <td><strong>Opening Balance</strong></td>
                            <td align="center"></td>
                            <td align="right"><%=(Double.parseDouble(openingBalance) + 0.00)%></td>
                        </tr>
                        <%}%>
                        <%
                        double totalBalance = 0.00, totalBalance1 = 0.00;
                        String receiptDate = "", amount = "", narration = "", accountHead = "";
                        ReceiptsVoucerDao rvdao = new ReceiptsVoucerDao();
                        ArrayList rv = rvdao.getListByAccountHead(accountHead1);
                        if (rv != null) {
                            System.out.println("Not Null rececipts");
                            Iterator itg = rv.iterator();
                            while (itg.hasNext()) {
                                ReceiptsVoucherBean gb = (ReceiptsVoucherBean) itg.next();
                                accountHead = gb.getReceipt_type();
                                receiptDate = gb.getReceipt_Date();
                                amount = gb.getAmount();
                                totalBalance = totalBalance + Double.parseDouble(amount);
                                narration = gb.getNarration();
                        %>
                        <tr>
                            <td align="center"><%=receiptDate%></td>
                            <td><%=accountHead%></td>
                            <td align="center">RV</td>
                            <td align="right"><%=(amount)%></td>
                        </tr>
                        <%}
                                          } else {

                                              System.out.println("rececipts else case");
                                              ArrayList rv1 = rvdao.getListByReceiptType(accountHead1);
                                              if (rv1 != null) {
                                                  Iterator itg1 = rv1.iterator();
                                                  while (itg1.hasNext()) {
                                                      System.out.println("Not Null -R");
                                                      ReceiptsVoucherBean gb1 = (ReceiptsVoucherBean) itg1.next();
                                                      accountHead = gb1.getAccount_head();
                                                      receiptDate = gb1.getReceipt_Date();
                                                      amount = gb1.getAmount();
                                                      totalBalance = totalBalance + Double.parseDouble(amount);
                                                      narration = gb1.getNarration();
                        %>
                        <tr >
                            <td height="19" align="center"><%=receiptDate%></td>
                            <td><%=accountHead%></td>
                            <td align="center">RV</td>
                            <td align="right"><%=(amount)%></td>
                        </tr>
                        <%}
                            }
                        }
                        %>
                        <!-- -->
                        <%
                        String jvno = "", amountType = "", rupeesc = "", rupeesd = "";
                        Vector jno = new Vector();
                        Vector amtyp = new Vector();
                        Vector rpcr = new Vector();
                        Vector rpdt = new Vector();
                        JvDao jvdao = new JvDao();
                        ArrayList jv = jvdao.getTransactionType(accountHead1);
                        if (jv != null) {
                            System.out.println("Not Null rececipts");
                            Iterator itg3 = jv.iterator();
                            while (itg3.hasNext()) {
                                JvBean jb = (JvBean) itg3.next();
                                amountType = jb.getAmount_type();
                                rupeesc = jb.getCredit_Amount();
                                rupeesd = jb.getDebit_Amount();
                                jvno = jb.getJv_No();
                                jno.add(jvno);
                                amtyp.add(amountType);
                                rpcr.add(rupeesc);
                                rpdt.add(rupeesd);
                            }
                        }

                        %>
                        <%
                        String jvdate = "", amountR = "";
                            for (int i = 0; i < jno.size(); i++) {
                                if (amtyp.elementAt(i).equals("Dr")) {
                            System.out.println("***************** Dr" + amountR);
                            JvDao jvdao1 = new JvDao();
                                ArrayList jv1 = jvdao1.getAccountHeadsList(accountHead1,"Cr", jno.elementAt(i).toString());
                                if (jv1 != null) {
                                    System.out.println("Not Null rececipts");
                                    Iterator itg4 = jv1.iterator();
                                    while (itg4.hasNext()) {
                                        JvBean gb = (JvBean) itg4.next();
                                        accountHead = gb.getAccount_Head();
                                        jvdate = gb.getJv_Date();
                                        amountR = gb.getCredit_Amount();
                                        System.out.println("amount R@############" + amountR);
                                        System.out.println("Dr amountn" + rpdt.elementAt(i).toString());
                                        if (Double.parseDouble(rpdt.elementAt(i).toString()) > Double.parseDouble(amountR)) {
                                            totalBalance = totalBalance + (Double.parseDouble(amountR));
                                        } else {
                                            totalBalance = totalBalance + Double.parseDouble(rpdt.elementAt(i).toString());
                                        }
                        %>
                        <tr>
                            <td><%=jvdate%></td>
                            <td><%=accountHead%></td>
                            <td>JV</td>
                            <%
                                                       if (Double.parseDouble(rpdt.elementAt(i).toString()) > Double.parseDouble(amountR)) {
                            %>
                            <td align="right"><%=(Double.parseDouble(amountR))%></td>
                            <%} else {%>
                            <td align="right"><%=Double.parseDouble(rpdt.elementAt(i).toString())%></td>
                            <%}%>
                        </tr>
                        <%    }
                                }
                            }
                        }
                        %>
                    </table></td>
                <td width="51%" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-right: #000000 1px solid;
                                                    border-top: #000000 1px solid; border-left: #000000 1px solid;  border-bottom: #000000 1px solid; font-size: 9pt; color: #000000; font-family: Arial;">
                        <tr>
                            <td colspan="4"><strong>Credit (Benifits Received By Us)</strong></td>
                        </tr>
                        <tr>
                            <th width="11%" height="24">Date</th>
                            <th width="13%">Description</th>
                            <th width="5%">Ref</th>
                            <th width="20%">Amount</th>
                        </tr>
                        <%
                        if (acheadtype.equals("Cr")) {
                        %>
                        <tr >
                            <td align="center">&nbsp;</td>
                            <td><strong>Opening Balance</strong></td>
                            <td align="center"></td>
                            <td align="right"><%=(Double.parseDouble(openingBalance) + 0.00)%></td>
                        </tr>
                        <%}%>
                        <%
                        String paymentDate = "";
                        PaymentsVoucerDao pvdao = new PaymentsVoucerDao();
                        ArrayList pv = pvdao.getPaymentsList(accountHead1);
                        if (pv != null) {
                            Iterator itg = pv.iterator();
                            while (itg.hasNext()) {
                                System.out.println("Not Null Payments");
                                PaymentVoucherBean pvb = (PaymentVoucherBean) itg.next();
                                accountHead = pvb.getPayment_type();
                                paymentDate = pvb.getPayment_date();
                                amount = pvb.getAmount();
                                totalBalance1 = totalBalance1 + Double.parseDouble(amount);
                                narration = pvb.getNarration();
                        %>
                        <tr>
                            <td align="center"><%=paymentDate%></td>
                            <td><%=accountHead%></td>
                            <td align="center">PV</td>
                            <td align="right"><%=amount%></td>
                        </tr>
                        <%}
                                              } else {
                                                  ArrayList pv1 = pvdao.getPaymentsListByType(accountHead1);
                                                  if (pv1 != null) {
                                                      Iterator itg2 = pv1.iterator();
                                                      while (itg2.hasNext()) {
                                                          System.out.println("Not Null -P");
                                                          PaymentVoucherBean pvb1 = (PaymentVoucherBean) itg2.next();
                                                          accountHead = pvb1.getAccount_head();
                                                          paymentDate = pvb1.getPayment_date();
                                                          amount = pvb1.getAmount();
                                                          totalBalance1 = totalBalance1 + Double.parseDouble(amount);
                                                          narration = pvb1.getNarration();
                        %>
                        <tr >
                            <td align="center"><%=paymentDate%></td>
                            <td><%=accountHead%></td>
                            <td align="center">PV</td>
                            <td align="right"><%=(amount)%></td>
                        </tr>
                        <%}
                            }
                        }
                        %>


                        <%
                        String amountRs = "";
        

                            for (int j = 0; j < jno.size(); j++) {
                                        if (amtyp.elementAt(j).equals("Cr")) {
                                JvDao jvdao2 = new JvDao();
                                System.out.println("--------------------" + j);
                                ArrayList jv1 = jvdao2.getAccountHeadsList(accountHead1,"Dr", jno.elementAt(j).toString());
                                if (jv1 != null) {
                                    Iterator itg4 = jv1.iterator();
                                    while (itg4.hasNext()) {
                                        System.out.println("Not Null rececipts !!!!!!!!");
                                        JvBean gb = (JvBean) itg4.next();
                                        accountHead = gb.getAccount_Head();
                                        jvdate = gb.getJv_Date();
                                        amountRs = gb.getDebit_Amount();
                        					
                                                                if (Double.parseDouble(rpcr.elementAt(j).toString()) > Double.parseDouble(amountRs)) {
                                            totalBalance1 = totalBalance1 + (Double.parseDouble(amountRs));
                                        } else {
                                            totalBalance1 = totalBalance1 + Double.parseDouble(rpcr.elementAt(j).toString());
                                        }
						
                        %>
                        <tr>
                            <td align="center"><%=jvdate%></td>
                            <td><%=accountHead%></td>
                            <td align="center">JV</td>
                            <%
                                                                           //out.println("********" + amountRs);
                                                  // out.println("********" + rpcr.elementAt(j).toString());
                                               if (Double.parseDouble(rpcr.elementAt(j).toString()) > Double.parseDouble(amountRs)) {
                            %>
                            <td align="right"><%=(Double.parseDouble(amountRs))%></td>
                            <%} else {%>
                            <td align="right"><%=Double.parseDouble(rpcr.elementAt(j).toString())%></td>
                            <%}%>
                        </tr>
                        <%}
                                }
                            }
                        }
                        %>
                    </table></td>
            </tr>
        </table>
        <%
                double closingBalance = 0.00, tb = 0.00;
                tb = totalBalance;
                if (totalBalance1 > totalBalance) {
                     totalBalance = totalBalance1;
                }
        %>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="49%" align="right" valign="top">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-right: #000000 1px solid;
                           border-top: #000000 1px solid; border-left: #000000 1px solid;  border-bottom: #000000 1px solid; font-size: 9pt; color: #000000; font-family: Arial;">
                        <%
                        int count = 0;
                        if (tb < totalBalance1) {
                            count = 1;
                            closingBalance = totalBalance1 - tb;
                        %>
                        <tr>
                            <td align="center"><strong>Closing Balance</strong></td>
                            <td align="right"><%=(closingBalance + Double.parseDouble(openingBalance))%></td>
                        </tr>
                        <%}
        if (count == 0) {%>
                        <tr>
                            <td align="center">&nbsp;</td>
                            <td align="right">&nbsp;</td>
                        </tr>
                        <%}%>
                        <tr>
                            <td width="51%" align="center"><strong>Amount</strong></td>
                            <td width="49%" align="right"><strong><%=(totalBalance + Double.parseDouble(openingBalance))%></strong></td>
                        </tr>
                    </table></td>
                <td width="51%" align="right" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-right: #000000 1px solid;
                                                                  border-top: #000000 1px solid; border-left: #000000 1px solid;  border-bottom: #000000 1px solid; font-size: 9pt; color: #000000; font-family: Arial;">
                        <%
                        int count1 = 0;
                        if (totalBalance1 < tb) {
                            count1 = 0;
                            closingBalance = tb - totalBalance1;
                        %>
                        <tr>
                            <td align="center"><strong>Closing Balance</strong></td>
                            <td align="right"><%=closingBalance + Double.parseDouble(openingBalance)%></td>
                        </tr>
                        <%}
        if (count1 == 0) {%>
                        <tr>
                            <td align="center">&nbsp;</td>
                            <td align="right">&nbsp;</td>
                        </tr>
                        <%}%>
                        <tr>
                            <td width="54%" align="center"><strong>Amount</strong></td>
                            <td width="46%" align="right"><strong><%=(totalBalance + Double.parseDouble(openingBalance))%></strong></td>
                        </tr>
                    </table></td>
            </tr>
        </table>
    </body>
</html>
